﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using InvoiceManagement.Support;
using System.Configuration;

namespace InvoiceManagement.Forms
{
  public partial class CompanyDetails : Form
  {
    private string connectionString = ConfigurationSettings.AppSettings["ConnectionString"];

    public CompanyDetails()
    {
      InitializeComponent();
      DatabaseOperations.Initialize(this.connectionString);
    }

    private void OnAddEntryClick(object sender, EventArgs e)
    {
      try
      {
        string query = "INSERT INTO CompanyDetails(CompanyName,TelephoneNumber,AlternateNumber,Address,Description) VALUES(@CompanyName,@TelephoneNumber,@AlternateNumber,@Address,@Description)";
        List<MySqlParameter> parameters = new List<MySqlParameter>();
        parameters.Add(new MySqlParameter("@CompanyName", this.txtCompanyName.Text));
        parameters.Add(new MySqlParameter("@TelephoneNumber", this.txtTelephoneNumber.Text));
        parameters.Add(new MySqlParameter("@AlternateNumber", this.txtAlternateNumber.Text));
        parameters.Add(new MySqlParameter("@Address", this.txtAddress.Text));
        parameters.Add(new MySqlParameter("@Description", this.txtDescription.Text));
        DatabaseOperations.ExecuteQueryWithParameters(query, parameters);

        MessageBox.Show("Company Information added successfully ...", "Inventory Management",MessageBoxButtons.OK,MessageBoxIcon.Information);
        this.DialogResult = DialogResult.OK;
      }
      catch (MySqlException ex)
      {
        Console.WriteLine("Error: {0}", ex.ToString());
        MessageBox.Show("Invalid Entry", "Inventory Management", MessageBoxButtons.OK, MessageBoxIcon.Error);
      }
    }

    private void OnCancelClick(object sender, EventArgs e)
    {
      this.Close();
    }
  }
}
